<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2018/12/3
 * Time: 11:16
 */

namespace App\Http\Controllers\v3;


use App\Models\Counts;
use App\Models\CountsLog;
use Illuminate\Support\Facades\Log;

class CountController extends Controller
{

    /**
     * 更新数据统计
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(){
        $id = (int)request('id',0);

        try{
            $count = Counts::where('id',$id)->first();
            $today = (string)date('Y-m-d',time());
            if($count){
                $countLog = CountsLog::where('cid',$id)->where('date',$today)->first();
                if($countLog){
                    //查找当天数据存在，更新+1
                    $countLog->increment('total');
                }else{
                    //查找当天数据不存在，创建
                    CountsLog::create(['cid'=>$id,'date'=>$today,'total'=>1,'pid'=>$count['pid']]);
                }
            }else{
                return $this->returnJson(1,'ERROR');
            }
            return $this->returnJson(0);
        }catch (\Exception $exception){
            Log::error($exception->getMessage());
            return $this->returnJson(1,'ERROR');
        }
    }

}
